/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.meanzoft.m3d.util;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

/**
 *
 * @author Meanz
 */
public class BufferUtils {

    public static final int SIZE_DOUBLE = 8;
    public static final int SIZE_FLOAT = 4;
    public static final int SIZE_INT = 4;
    public static final int SIZE_BYTE = 1;

    public static ByteBuffer allocBytes(int howmany) {
        return ByteBuffer.allocateDirect(howmany * SIZE_BYTE).order(ByteOrder.nativeOrder());
    }

    public static IntBuffer allocInts(int howmany) {
        return ByteBuffer.allocateDirect(howmany * SIZE_INT).order(ByteOrder.nativeOrder()).asIntBuffer();
    }

    public static FloatBuffer allocFloats(int howmany) {
        return ByteBuffer.allocateDirect(howmany * SIZE_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
    }

    public static DoubleBuffer allocDoubles(int howmany) {
        return ByteBuffer.allocateDirect(howmany * SIZE_DOUBLE).order(ByteOrder.nativeOrder()).asDoubleBuffer();
    }

    public static ByteBuffer allocBytes(byte[] bytearray) {
        ByteBuffer bb = ByteBuffer.allocateDirect(bytearray.length * SIZE_BYTE).order(ByteOrder.nativeOrder());
        bb.put(bytearray).flip();
        return bb;
    }

    public static IntBuffer allocInts(int[] intarray) {
        IntBuffer ib = ByteBuffer.allocateDirect(intarray.length * SIZE_FLOAT).order(ByteOrder.nativeOrder()).asIntBuffer();
        ib.put(intarray).flip();
        return ib;
    }

    public static FloatBuffer allocFloats(float[] floatarray) {
        FloatBuffer fb = ByteBuffer.allocateDirect(floatarray.length * SIZE_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
        fb.put(floatarray).flip();
        return fb;
    }

    public static DoubleBuffer allocDoubles(double[] darray) {
        DoubleBuffer fb = ByteBuffer.allocateDirect(darray.length * SIZE_DOUBLE).order(ByteOrder.nativeOrder()).asDoubleBuffer();
        fb.put(darray).flip();
        return fb;
    }

    public static void put(ByteBuffer b, byte[] values) {
        b.clear();
        b.put(values).flip();
    }

    public static void put(IntBuffer b, int[] values) {
        b.clear();
        b.put(values).flip();
    }

    public static void put(FloatBuffer b, float[] values) {
        b.clear();
        b.put(values).flip();
    }

    public static void put(DoubleBuffer b, double[] values) {
        b.clear();
        b.put(values).flip();
    }

    /**
     *  copy ints from the given byteBuffer into the given int array.
     *  @param b       source ByteBuffer
     *  @param values  target integer array, must be same length as ByteBuffer capacity/4
     */
    public static void get(ByteBuffer b, int[] values) {
        b.asIntBuffer().get(values, 0, values.length);
    }

    /**
     *  copy ints from the given IntBuffer into the given int array.
     *  @param b       source IntBuffer
     *  @param values  target integer array, must be same length as IntBuffer
     */
    public static void get(IntBuffer b, int[] values) {
        b.get(values, 0, values.length);
    }

    /**
     *  return the contents of the byteBuffer as an array of ints.
     *  @param b  source ByteBuffer
     */
    public static int[] getInts(ByteBuffer b) {
        int[] values = new int[b.capacity() / SIZE_INT];
        b.asIntBuffer().get(values, 0, values.length);
        return values;
    }
}
